package com.iafme.controller;

import cn.hutool.core.convert.Convert;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.ExamineReqDTO;
import com.iafme.dto.res.CertificateImagesResDTO;
import com.iafme.dto.res.ExamineResDTO;
import com.iafme.component.ExamineService;
import com.iafme.model.CertificateOrderImage;
import com.iafme.service.CertificateOrderImageService;
import com.systemframework.constants.Constants;
import com.systemframework.util.SearchUtl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/26 21:02
 */
@RestController
@RequestMapping(value = "/examine")
@AllArgsConstructor
public class ExamineController {

    private final BeanSearcher beanSearcher;

    private final ExamineService examineService;

    @PostMapping(value = "/lists")
    public Page<ExamineResDTO> lists(@RequestBody ExamineReqDTO examineReqDTO) {
        Map<String, Object> build = MapUtils.builder().page(examineReqDTO.getPageNum() - 1, examineReqDTO.getPageSize())
                .field(ExamineResDTO::getMemberName, examineReqDTO.getMemberName()).op(Contain.class)
                .field(ExamineResDTO::getAuthorization, examineReqDTO.getAuthorization1()).op(Equal.class)
                .field(ExamineResDTO::getCertificateName, examineReqDTO.getCertificateName()).op(Contain.class)
                .field(ExamineResDTO::getStatus,Constants.ONE).op(Equal.class)
                .build();
        return SearchUtl.searchDataList(examineReqDTO.getPageNum(), examineReqDTO.getPageSize(),beanSearcher.search(ExamineResDTO.class,build));
    }

    private final CertificateOrderImageService imageService;


    @GetMapping(value = "/findExamineById/{id}")
    public ExamineResDTO findExamineById(@PathVariable Integer id) {
        Map<String, Object> build = MapUtils.builder().field(ExamineResDTO::getId, id).build();
        ExamineResDTO examineResDTO = beanSearcher.searchFirst(ExamineResDTO.class, build);
        LambdaQueryWrapper<CertificateOrderImage> eq = Wrappers.lambdaQuery(CertificateOrderImage.class).eq(CertificateOrderImage::getOrderId, id);
        List<CertificateOrderImage> list = imageService.list(eq);
        examineResDTO.setCertificateImages(Convert.toList(CertificateImagesResDTO.class, list));
        return examineResDTO;
    }



    @PostMapping(value = "/examineOrder")
    public String examineOrder(@RequestBody ExamineReqDTO examineReqDTO) {
        examineService.examineOrder(examineReqDTO);
        return Constants.SUCESS;
    }
}
